//
//  YFSelectCityView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/4/13.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFSelectCityView.h"

@interface YFSelectCityView()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIView *coverView; // 背景遮盖
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *cityArr;
@end

@implementation YFSelectCityView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, Height_NavBar, UISCREEN_WIDTH, UISCREEN_HEIGHT - Height_NavBar);
        [self makeView];
        
        // 默认隐藏
        [self hidden];
    }
    return self;
}

- (void)makeView {
    // 启动懒加载
    self.coverView;
    self.tableView;
}

#pragma mark - 公用方法
- (void)show {
    self.hidden = NO;
    self.isShowing = YES;
    
    if (self.cityArr.count == 0) {
        [self dataRequestCityAction];
    }
}

- (void)hidden {
    self.hidden = YES;
    self.isShowing = NO;
}
#pragma mark - 网络请求
- (void)dataRequestCityAction {
    
    [WWNetRequest fetchCityListWithParameters:nil success:^(id response) {
        
        NSArray *dataArr = response[@"data"];
        NSMutableArray *tempArr = [NSMutableArray array];
        for (NSDictionary *dict in dataArr) {
            CityModel *model = [[CityModel alloc]initWithDictionary:dict error:nil];
            [tempArr addObject:model];
        }
        
        // 城市赋值
        self.cityArr = [NSArray arrayWithArray:(NSArray *)tempArr];
        
        [self.tableView reloadData];
        
    } failure:^(NSDictionary *error) {
        NSLog(@"城市列表 error = %@", error);
    }];
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cityArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CityModel *model = self.cityArr[indexPath.row];
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = model.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CityModel *model = self.cityArr[indexPath.row];

    if (self.didSelectRow) {
        self.didSelectRow(model);
    }
    
    // 缓存城市
    if (model.name != 0) {

        [YFSaveTool userDefaults:model.name key:@"selectedCity"];

//        // 测试
//        NSString *cityName = [defaults objectForKey:@"selectedCity"];
//        NSLog(@"cityName = %@", cityName);
    }
    
    // 相当于点击遮盖, 需要重置按钮的selected值
    [self hidden];
    
    if (model.code.length == 0) {
        return;
    }
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch {
    
    // 解决"手势"导致 didSelectRowAtIndexPath不调用
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return YES;
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.frame = CGRectMake(0, 0, self.coverView.frame.size.width/2, self.coverView.frame.size.height/2);
//        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.coverView addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}

-(UIView *)coverView {
    if (!_coverView) {
        _coverView = [[UIView alloc] init];
        _coverView.frame = self.bounds;
        _coverView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.3];
        [self addSubview:_coverView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
    }
    return _coverView;
}

- (void)coverClick {
    KMyLog(@"点击了遮盖");
    [self hidden];
}

@end
